package com.dckeji.shiplock.webapp.dao.impl;

import com.dckeji.shiplock.webapp.dao.IDeclareDao;
import com.dckeji.shiplock.webapp.pojo.Declare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/9
 * Time: 10:00
 */
@Repository
public class DeclareDaoImpl implements IDeclareDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int commitDeclare(Declare declare) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO decla ").append("(shipType,shipNo,shipCourse,toWingCount,isLoading,goodsName,isDanger,")
                .append("isOverload,beginHarbor,endHarbor,fleetLength,weightTotal,barges,applicant,phone,commitTime,maxdraft)")
                .append(" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");


        int updateRow = jdbcTemplate.update(sql.toString(), new Object[]{declare.getShipType(), declare.getShipNo()
                , declare.getShipCourse()
                , declare.getToWingCount(), declare.getIsLoading(), declare.getGoodsName(), declare.getIsDanger(),
                declare.getIsOverload(), declare.getBeginHarbor(), declare.getEndHarbor(), declare.getFleetLength(),
                declare.getWeightTotal(), declare.getBarges(), declare.getApplicant(), declare.getPhone()
                , declare.getCommitTime(), declare.getMaxdraft()
        });
        return updateRow;
    }

    @Override
    public List<Declare> query(String shipNo, Integer status) {
        String sql = "select * from decla where shipNo=?  and isExamine=?";
        List<Declare> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Declare>(Declare.class), new Object[]{shipNo, status});
        return query;
    }

    @Override
    public List<Declare> queryDeclareList() {

        String sql = "select * from decla  where isExamine=0";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Declare>(Declare.class));
    }
}
